package com.gem.eolane.web;

import com.gem.eolane.config.AppConfig;
import com.gem.eolane.entity.PcbOffer;
import com.gem.eolane.entity.User;
import com.gem.eolane.service.NewsService;
import com.gem.eolane.service.PcbOfferService;
import com.gem.eolane.service.SurfaceService;
import com.gem.eolane.service.UserService;
import com.gem.eolane.utils.Enande;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gem.eolane.entity.News;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.servlet.http.HttpSession;
import java.util.Random;
import com.gem.eolane.entity.Contactus;
import com.gem.eolane.service.ContactusService;

//前台放行
@Controller
@RequestMapping("/f/t")
public class TReceInterfaceController {
    @Autowired
    private NewsService newsService;

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private SurfaceService surfaceService;

    public static String Codes;

    @Autowired
    private AppConfig appConfig;

    @Autowired
    PcbOfferService pcbOfferService;

    @Autowired
    private UserService userService;

    //=====================李jingyang========================

    //跳转用户注册
    @RequestMapping("/register")
    public String register(){
        return "surfacepage/register";
    }

    //注册用户
    @RequestMapping("/registerUser")
    public String registerUser(User user, Model model){
        boolean flag = userService.findUserByPhoneNumber(user.getPhoneNumber());
        if(flag==true){
            model.addAttribute("flag",flag);
            return "surfacepage/register";
        }
        //注册业务
        //密码进行加密，，关键key为：private static String password = "小子来破解啊";
        String password = Enande.encrypt(user.getPassword());
        userService.insertUser(user.getUserName(),password,user.getPhoneNumber(),user.getSex(),user.getCompany(),user.getMail());
        model.addAttribute("registerUser",user);
        return "redirect:login";
    }

    //前台通过邮箱注册
    @ResponseBody
    @RequestMapping("/findregister")
    public String sendSimpleMail( String email) throws Exception {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(appConfig.mail);
        message.setTo(email);
        message.setSubject("主题：验证密码");
        String code = "";
        Random random = new Random();
        for(int i=0;i<6;i++) {
            int r = random.nextInt(10);
            code += r;
        }
        message.setText("随机验证码是:"+code);
        mailSender.send(message);
        //  System.out.println("++++++++==================code+++==:"+code);
        return code;
    }


    //=====================李jingyang========================


    //=====================徐jianghui========================
    //通过邮箱找回密码1
    @RequestMapping("/finpo")
    public String finpo(){
        return "surfacepage/forgetPwd1";
    }


    //通过邮箱找回密码2
    @RequestMapping("/finpt")
    public String finpt(String email, String vaeify, String verify1, HttpSession session){
        String mail = surfaceService.finuse(email);
        if (mail!=null){
            if (vaeify.equals(verify1)){
                session.setAttribute("mail",mail);
                return "surfacepage/forgetPwd2";
            }else {
                return "surfacepage/forgetPwd1";
            }
        }
        return "redirect:finpo";
    }


    //发送验证码
    @ResponseBody
    @RequestMapping("/findpwds")
    public String sendSimpleMail(HttpSession session){
        String mail = (String) session.getAttribute("mail");
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(appConfig.mail);
        message.setTo(mail);
        message.setSubject("主题：验证密码");;
        String code = "";
        Random random = new Random();
        for(int i=0;i<6;i++) {
            int r = random.nextInt(10);
            code += r;
        }
        Codes =code;
        message.setText("随机验证码是:"+code);
        mailSender.send(message);
        return "success";
    }

    //通过邮箱找回密码3
    @ResponseBody
    @RequestMapping("/finps")
    public String finps(String uname){
        System.out.println("......"+uname);
        System.out.println("==="+Codes);
        if (Codes.equals(uname)){
            return "success";
        }else {
            return "false";
        }
    }

    //通过邮箱找回密码4
    @RequestMapping("/finpth")
    public String finpf(){

        return "surfacepage/forgetPwd3";
    }

    //通过邮箱找回密码5
    @RequestMapping("/finpf")
    public String finpts(HttpSession session, HttpServletRequest request) {
        String Epwd = request.getParameter("pwd");
        String pwd = Enande.encrypt(Epwd);
        String mail = (String)session.getAttribute("mail");
        surfaceService.upword(pwd,mail);
        System.out.println(pwd);
        /* surfaceService.upword(password,mail);*/
        return "surfacepage/forgetPwd4";
    }

    //=====================徐jianghui========================


    //=========================袁zhihao=====================


    //首页
    @RequestMapping("/showmain")
    public String showIndex(HttpServletRequest request, User user, Model model){
        HttpSession session = request.getSession();
        user = (User) session.getAttribute("user");
        System.out.println("首页"+user);
        boolean flags = true;
        if (user==null){

            model.addAttribute("flags",flags);
            return "surfacepage/index";
        }
        model.addAttribute("photoAddress",user.getPhotoAddress());
        flags = false;
        model.addAttribute("flags",flags);
        System.out.println(user.getPhotoAddress());
        return "surfacepage/index";
    }

    //其他页面进pcb报价
    @RequestMapping("/pcboffer")
    public String pcboffer(Model model,HttpServletRequest httpServletRequest){
        User user = (User) httpServletRequest.getSession().getAttribute("user");
        model.addAttribute("user",user);
        model.addAttribute("layer","0");
        return "surfacepage/pcboffer";
    }

    //主页面进pcb报价
    @RequestMapping("/pcboffer1")
    public String pcboffer1(HttpServletRequest httpServletRequest, Model model){
        Integer length = null;
        Integer width = null;
        if (httpServletRequest.getParameter("length")==""){
        }else{
            length = Integer.parseInt(httpServletRequest.getParameter("length"));
        }
        if (httpServletRequest.getParameter("width")==""){
        }else{
            width = Integer.parseInt(httpServletRequest.getParameter("width"));
        }
        /*User user =  (User) httpServletRequest.getSession().getAttribute("user");*/
        String layer = httpServletRequest.getParameter("layer");
        Integer num = Integer.parseInt(httpServletRequest.getParameter("num"));
        String thinkness = httpServletRequest.getParameter("thinkness");
        User user = (User) httpServletRequest.getSession().getAttribute("user");
        model.addAttribute("user",user);
        model.addAttribute("length",length);
        model.addAttribute("width",width);
        model.addAttribute("layer",layer);
        model.addAttribute("num",num);
        model.addAttribute("thinkness",thinkness);
        return "surfacepage/pcboffer";
    }

    /*计算报价*/
    @RequestMapping("/pcbresult")
    public String pcbres(PcbOffer pcbOffer,Model model,HttpSession session){
        User user =  (User) session.getAttribute("user");

        //存储匿名报价信息
        PcbOffer sessionPcbOffer = (PcbOffer) session.getAttribute("pcbOffer");
        if (sessionPcbOffer == null) {
            session.setAttribute("pcbOffer",pcbOffer);
            sessionPcbOffer = pcbOffer;
        }
        System.out.println("sessionPcbOffer =========>" + sessionPcbOffer);
        sessionPcbOffer = pcbOfferService.pcbCount(sessionPcbOffer);

        model.addAttribute("user",user);
//        System.out.println("***************************3"+pcbOffer.getCreateTime());
        model.addAttribute("pcbOffer",sessionPcbOffer);
        return "surfacepage/pcbresult";
    }

    //=========================袁zhihao=====================


    //========================蔡zhuang======================
    //公司简介(欧朗集团)
    @RequestMapping("/about")
    public String about() {
        return "surfacepage/about";
    }

    //欧朗中国
    @RequestMapping("/about1")
    public String about1() {
        return "surfacepage/about_1";
    }

    //联系我们
    @RequestMapping("/contactus")
    public String contactus() {
        return "surfacepage/contactus";
    }

    //公司简介头部
    @RequestMapping("/header5")
    public String header5(HttpSession session, HttpServletRequest request, Model model) {
        boolean flags = true;
        session =  request.getSession();
        User user = (User) session.getAttribute("user");
        if(user == null){
            model.addAttribute("flags",flags);
            return "common/header5";
        }
        flags = false;
        model.addAttribute("flags",flags);
        model.addAttribute("photoAddress",user.getPhotoAddress());
        return "common/header5";
    }

    //联系我们头部
    @RequestMapping("/header6")
    public String header6(HttpSession session,HttpServletRequest request,Model model) {
        boolean flags = true;
        session =  request.getSession();
        User user = (User) session.getAttribute("user");
        if(user == null){
            model.addAttribute("flags",flags);
            return "common/header6";
        }
        flags = false;
        model.addAttribute("flags",flags);
        model.addAttribute("photoAddress",user.getPhotoAddress());
        return "common/header6";
    }


    @Autowired
    private ContactusService contactusService;

    //联系我们
    @ResponseBody
    @RequestMapping("/contactus1")
    public String contactus1(Contactus contactus) throws UnsupportedEncodingException {
        return contactusService.contact1(contactus);
    }

    //我要入住
    @ResponseBody
    @RequestMapping("/contactus2")
    public String contactus2(Contactus contactus) throws UnsupportedEncodingException {
        return contactusService.contact2(contactus);
    }

    //氛围灯合作
    @ResponseBody
    @RequestMapping("/contactus3")
    public String contactus3(Contactus contactus) throws UnsupportedEncodingException {
        return contactusService.contact3(contactus);
    }

    //ODM字段联系
    @ResponseBody
    @RequestMapping("/contactus4")
    public String contactus4(Contactus contactus) throws UnsupportedEncodingException {
        return contactusService.contact4(contactus);
    }
    //========================蔡zhuang======================

    //========================刘hui=========================
    //登录
    @RequestMapping("/login")
    public String login(){
        return "surfacepage/login";
    }

    //登录密码用户名判断
    @RequestMapping("/fLogin")
    @ResponseBody
    public String list(String phoneNumber, String password, Model model, HttpSession session){
        User user = userService.findQueryUser(phoneNumber);
        String flag = "";
        System.out.println(user);

        if(user !=null) {
            if(user.getDelIdent()== 1) {
                flag = "2";//账号锁定
            } else {
                if (Enande.decrypt(user.getPassword()).equals(password)) {
                    session.setAttribute("user",user);
                    model.addAttribute("photoAddress",user.getPhotoAddress());
                    return "1";//  登录成功
                } else {
                    flag = "3";//用户名或密码错误
                }
            }
        } else {
            flag = "3"; //用户名或密码错误
        }
        return flag;
    }


    //EMS
    @RequestMapping("/EMS")
    public String EMS(){
        return "surfacepage/EMS";
    }
    @RequestMapping("/EMS_CFT")
    public String emscft(){
        return "surfacepage/EMS_CFT";
    }

    @RequestMapping("/EMS_DOS")
    public String emsdos(){
        return "surfacepage/EMS_DOS";
    }

    @RequestMapping("/EMS_FPS")
    public String emsfps(){
        return "surfacepage/EMS_FPS";
    }

    @RequestMapping("/EMS_M")
    public String emsm(){
        return "surfacepage/EMS_M";
    }

    @RequestMapping("/EMS_QMS")
    public String emsqms(){
        return "surfacepage/EMS_QMS";
    }

    @RequestMapping("/EMS_RD")
    public String emsrd(){
        return "surfacepage/EMS_RD";
    }

    @RequestMapping("/EMS_SC")
    public String emssc(){
        return "surfacepage/EMS_SC";
    }

    @RequestMapping("/EMS_Test")
    public String emsTest(){
        return "surfacepage/EMS_Test";
    }

    //ODM客制化
    @RequestMapping("/smartF")
    public String smartF(){
        return "surfacepage/smart_factory";
    }
    //========================刘hui=========================


    //在首页尾部点新闻进入的控制器
    @RequestMapping("/news/companyNews")
    public String findCompanyNews(Model model, String category, HttpServletRequest request) throws UnsupportedEncodingException {

        List<News> companyList = null;
        News news = new News();
        int total,pageCount;
        request.setCharacterEncoding("utf-8");
        //左侧导航栏公司新闻标题
        List<News> companyNewsList = newsService.findCompanyNewsDesc();
        // System.out.println("数据库查询出来的数据"+companyNewsList);
        //左侧导航栏行业新闻标题
        List<News> industryNewsList = newsService.findIndustryNewsDesc();
        //最新的一条公司新闻
        int pageNo = 1 ;
        int pageSize = 6 ;
        String pageNoStr = request.getParameter("pageNo");

        //判断页面传来的值
        if(pageNoStr!=null && !"".equals(pageNoStr)){
            pageNo = Integer.parseInt(pageNoStr);
        }
        String pageSizeStr = request.getParameter("pageSize");
        //  System.out.println("pageSize值"+pageSize);

        if(pageSizeStr!=null && !"".equals(pageSizeStr)){
            pageSize = Integer.parseInt(pageSizeStr);
        }

        int categorys = Integer.parseInt(category);
        //标识唯一行业新闻
        if(categorys==1){
            //行业新闻
            // System.out.println("进入行业新闻+pageno"+pageNo);
            companyList   =newsService.findPageIndustryNews(pageNo,pageSize);
            //非空判断，如果集合为空，给news复制为null
            if(companyList.isEmpty()||(companyList.size()==0)){
                news = null;
            }else {
                news = companyList.get(0);
            }
            total = newsService.getIndustryTotal();
            pageCount = ( total + pageSize-1)/pageSize;
            model.addAttribute("pageCount",pageCount);


        }
        //标识为零，进入公司新闻
        if (categorys==0){
            //公司新闻
            // System.out.println("进入公司新闻");
            companyList   =newsService.findPageCompanyNews(pageNo,pageSize);
            //非空判断
            if(companyList.isEmpty()||(companyList.size()==0)){
                news = null;
            }else {
                news = companyList.get(0);
            }
            total = newsService.getCompanyTotal();
            pageCount = ( total + pageSize-1)/pageSize;
            model.addAttribute("pageCount",pageCount);
        }

        //news_list.html最下端新闻列表
        model.addAttribute("pageNo",pageNo);
        model.addAttribute("companyList",companyList);

        model.addAttribute("pageSize", pageSize);

        model.addAttribute("news",news);
        model.addAttribute("industryNewsList",industryNewsList);
        model.addAttribute("companyNewsList",companyNewsList);

        //  System.out.println("list数据"+companyList);
        //返回视图
        return "surfacepage/news_list";
    }
    //根据id查询新闻内容
    @RequestMapping("/news/newsContent")
    public String findContent(Model model,String id){
        System.out.println("进来id"+id);
        //左侧导航栏公司新闻标题
        List<News> companyNewsList = newsService.findCompanyNewsDesc();
        //左侧导航栏行业新闻标题
        List<News> industryNewsList = newsService.findIndustryNewsDesc();

        int Id = Integer.parseInt(id);
        News news = newsService.findNewsContent(Id);

        model.addAttribute("industryNewsList",industryNewsList);
        model.addAttribute("companyNewsList",companyNewsList);
        model.addAttribute("news",news);
        return "surfacepage/news_detail";

    }
    //智能孵化器
    @RequestMapping("/incubator")
    public String incubator(){
        return "surfacepage/incubator";
    }
    //通过afternoon访问Afternoon_tea.html

    @RequestMapping("/afternoon")
    public String findAfeternoon(){
        return "surfacepage/Afternoon_tea";
    }

    //通过/jions访问jion.html
    @RequestMapping("/jions")
    public String findJion(){
        // System.out.println("进入");
        return "surfacepage/join";

    }

    //========================程guocheng====================

    //========================公共页面=======================
    /*导入头*/
    @RequestMapping("/header")
    public String header(HttpSession session,HttpServletRequest request,Model model) {
        boolean flags = true;
        session =  request.getSession();
        User user = (User) session.getAttribute("user");
        if(user == null){
            model.addAttribute("flags",flags);
            return "common/header";
        }
        flags = false;
        model.addAttribute("flags",flags);
        model.addAttribute("photoAddress",user.getPhotoAddress());
        return "common/header";
    }

    @RequestMapping("/header1")
    public String header1(HttpSession session,HttpServletRequest request,Model model) {
        boolean flags = true;
        session =  request.getSession();
        User user = (User) session.getAttribute("user");
        if(user == null){
            model.addAttribute("flags",flags);
            return "common/header1";
        }
        flags = false;
        model.addAttribute("flags",flags);
        model.addAttribute("photoAddress",user.getPhotoAddress());
        return "common/header1";
    }
    //引头
    @RequestMapping("/header4")
    public String header4(HttpSession session,HttpServletRequest request,Model model) {
        boolean flags = true;
        session =  request.getSession();
        User user = (User) session.getAttribute("user");
        if(user == null){
            model.addAttribute("flags",flags);
            return "common/header4";
        }
        flags = false;
        model.addAttribute("flags",flags);
        model.addAttribute("photoAddress",user.getPhotoAddress());
        return "common/header4";
    }


    //底部
    @RequestMapping("/footer")
    public String footer() {
        return "common/footer";
    }

    //右侧导航栏
    @RequestMapping("/right")
    public String right() {
        return "common/rightwindow";
    }

    /*公共部分controller*/
    @RequestMapping("/header2")
    public String header2(HttpSession session,HttpServletRequest request,Model model) {
        boolean flags = true;
        session =  request.getSession();
        User user = (User) session.getAttribute("user");
        if(user == null){
            model.addAttribute("flags",flags);
            return "common/header2";
        }
        flags = false;
        model.addAttribute("flags",flags);
        model.addAttribute("photoAddress",user.getPhotoAddress());
        return "common/header2";
    }




    /*//新闻
    @RequestMapping("/news_list")
    public String news_list(){
        return "surfacepage/news_list";
    }*/


    /*  *//*新闻板块controller*//*
    @RequestMapping("/newsList")
    public String news(){
        return "/surfacepage/news_list";
    }*/

/*    @RequestMapping("/newsDetail")
    public  String newsd(){
        return "/surfacepage/news_detail";
    }*/


    /*smart板块controller*/
    @RequestMapping("/smartC")
    public String smartC(){
        return "surfacepage/smart_city";
    }
    @RequestMapping("/smartl")
    public String smartL(){
        return "surfacepage/smart_light";
    }
    @RequestMapping("/smartCar")
    public String smartCar(){
        return "surfacepage/carLightCooperation";
    }


}
